
import apis from '../../http/apis/apis'
import jwtDecode from "jwt-decode";
export default {
    namespaced: 'lessors',
    state: {
        lessorsData: [],
        // 获取登录的用户
        token: "",
        accountID: '',
        total: ''
    },
    mutations: {
        // changeLessorsData(state, data) {
        //     state.lessorsData = data
        // },
        // 获取到token
        changegetToken(state, newToken) {
            console.log(1, newToken);
            state.token = newToken;
        },
        changetTokenID(state, newTokenId) {
            state.accountID = newTokenId
        },
        changeLessorsData(state, data) {
            state.lessorsData = data.result
            state.total = data.total - 1
        },
        tokenGet(state){
            const token = localStorage.getItem("token");
            const decode = jwtDecode(token);
            console.log(decode);
            state.token=decode.account
        }
    },
    actions: {
        async getLessors(context) {
            const { data } = await apis.lessors.getLessors()
            context.commit('changeLessorsData', data.data)
        },
        // 获取token
        // getToken(context) {
        //     console.log(1);
        //     const token = localStorage.getItem("token");
        //     const decode = jwtDecode(token);
        //     console.log(decode.account);
        //     context.commit('changegetToken', decode.account)
        // },
        // 根据token获取到对应的ID
        async getTokenID(context) {
            console.log(12, context.state.token);
            const { data } = await apis.lessors.getTokenID({
                account: context.state.token,
            });

            if (data.status) {
                context.commit("changetTokenID", data.data._id)
                localStorage.setItem("accountId", data.data._id)
            }
        },
        async getLessors(context, getData) {
            const { data } = await apis.lessors.getLessors(getData)
            console.log(data);
            context.commit('changeLessorsData', data.data)
        },
        async frozenLessors(context, data) {
            const { result } = await apis.lessors.frozenLessors(data)
        }
    },
}